package com.example.homework.adapter;

import android.media.MediaPlayer;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.homework.R;
import com.example.homework.bean.VideoData;

import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
    private List<VideoData>videoData;

    public VideoAdapter(List<VideoData>videoData) {
        this.videoData=videoData;
    }
    @NonNull
    @Override
    public VideoAdapter.VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VideoViewHolder(LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_videos,parent,false));
    }

    @Override
    public void onBindViewHolder(VideoAdapter.VideoViewHolder holder, int position) {
         holder.setData(videoData.get(position));
    }

    @Override
    public int getItemCount() {
        return videoData.size();
    }
    static class VideoViewHolder extends RecyclerView.ViewHolder{
        VideoView videoView;
        ProgressBar progressBar;
        TextView textViewTitle,videoViewSubtitle;
        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            videoView=itemView.findViewById(R.id.videoView4);
            textViewTitle=itemView.findViewById(R.id.textViewTitle);
            videoViewSubtitle = itemView. findViewById(R.id.textViewSubtitle);
            progressBar = itemView. findViewById(R.id.progressBar);
        }
        public void  setData(VideoData videoData){
            textViewTitle.setText(videoData.videoTitle);
            videoViewSubtitle.setText(videoData.videoSubtitle);
            videoView.setVideoPath(videoData.videoURL);
            videoView.setOnPreparedListener(mediaPlayer -> {
                progressBar.setVisibility(View.GONE);
                mediaPlayer.start();
            });
            videoView.setOnCompletionListener(MediaPlayer::start);

        }
    }
}
